package com.example.chasshelper.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.chasshelper.AddTestActivity;
import com.example.chasshelper.CreateTestActivity;
import com.example.chasshelper.R;
import com.example.chasshelper.adapter.TestTeaAdapter;
import com.example.chasshelper.dao.ClassroomDao;
import com.example.chasshelper.dao.CourseDao;
import com.example.chasshelper.dao.MessageDao;
import com.example.chasshelper.dao.RegistrationDao;
import com.example.chasshelper.dao.SolutionDao;
import com.example.chasshelper.dao.StudentDao;
import com.example.chasshelper.dao.TeacherDao;
import com.example.chasshelper.dao.TestDao;
import com.example.chasshelper.model.Course;
import com.example.chasshelper.model.Student;
import com.example.chasshelper.model.Teacher;
import com.example.chasshelper.model.Test;
import com.example.chasshelper.utils.DaoUtils;

import java.util.ArrayList;
import java.util.List;

public class TestTeaFragment extends Fragment implements View.OnClickListener{

    // TODO: Rename parameter arguments, choose names that match
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private View view;
    private Activity mActivity;
    private Context mContext;
    //控件
    private RecyclerView rv_test;
    private ImageView iv_addToClassroom;
    private ImageView iv_create;
    private EditText et_factor;
    private ImageView iv_search;
    private TextView tv_myTest;
    //数据
    private List<Test> tests;
    private ArrayList<Test> addTest=new ArrayList<>();
    private Student student;
    private Teacher teacher;
    //dao
    private TeacherDao teacherDao= DaoUtils.getTeacherDao();
    private StudentDao studentDao=DaoUtils.getStudentDao();
    private CourseDao courseDao=DaoUtils.getCourseDao();
    private ClassroomDao classroomDao=DaoUtils.getClassroomDao();
    private RegistrationDao registrationDao=DaoUtils.getRegistrationDao();
    private SolutionDao solutionDao = DaoUtils.getSolutionDao();
    private MessageDao messageDao = DaoUtils.getMessageDao();
    private TestDao testDao = DaoUtils.getTestDao();
    //adapter
    private TestTeaAdapter testTeaAdapter;
    public TestTeaFragment() {
    }

    // TODO: Rename and change types and number of parameters
    public static TestTeaFragment newInstance(String param1, String param2) {
        TestTeaFragment fragment = new TestTeaFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // 绑定布局
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_message, container, false);
        }
        mActivity = getActivity();
        mContext = getContext();
        initUI();
        initData();
        return view;
    }

    private void initUI() {
        //绑定控件
        rv_test = view.findViewById(R.id.rv_test);
        iv_addToClassroom = view.findViewById(R.id.iv_addToClassroom);
        iv_create = view.findViewById(R.id.iv_create);
        et_factor = view.findViewById(R.id.et_factor);
        iv_search = view.findViewById(R.id.iv_search);
        tv_myTest = view.findViewById(R.id.tv_myTest);

        //点击事件
        iv_addToClassroom.setOnClickListener(this);
        iv_create.setOnClickListener(this);
        iv_search.setOnClickListener(this);
        tv_myTest.setOnClickListener(this);
    }
    private void initData() {
        //接收数据
        mActivity = getActivity();
        mContext = getContext();
        Bundle bundle = this.getArguments();
        teacher = (Teacher) bundle.getSerializable("teacher");
        System.out.println("TestTeaFragment:"+teacher);
        //dao获取数据
        tests = testDao.findAll();
        System.out.println("主页面的题目数据"+ tests);
        //adapter初始化，并向adapter传递数据
        testTeaAdapter = new TestTeaAdapter(mContext, tests,teacher,null);
        rv_test.setLayoutManager(new LinearLayoutManager(mContext));
        rv_test.setAdapter(testTeaAdapter);
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.iv_addToClassroom://添加题目到课堂
                for (int position = 0; position < rv_test.getLayoutManager().getItemCount(); position++) {
                    View view1 =  rv_test.getChildAt(position);
                    CheckBox cb_select = (CheckBox) view1.findViewById(R.id.cb_select);
                    TextView textView = (TextView) view1.findViewById(R.id.tv_id);
                    Integer id = Integer.valueOf(textView.getText().toString());
                    if(cb_select.isChecked()) {
                        addTest.add(testDao.findById(id));
                    }
                }
                intent.putExtra("addTest", addTest);
                intent.putExtra("teacher", teacher);
                intent.setClass(mContext, AddTestActivity.class);
                startActivity(intent);
                break;
            case R.id.iv_create://创建题目
                intent.putExtra("teacher", teacher);
                intent.setClass(mContext, CreateTestActivity.class);
                startActivity(intent);
                break;
            case R.id.iv_search://搜索
                tests =testDao.findByIdOrTestContextLike(et_factor.getText().toString());
                testTeaAdapter.setList(tests);
                testTeaAdapter.notifyDataSetChanged();
                break;
            case R.id.tv_myTest://搜索我的题目
                tests = testDao.findByTeacherId(teacher);
                testTeaAdapter.setList(tests);
                testTeaAdapter.notifyDataSetChanged();
                break;

        }
    }
}